package com.example.kaoqinmanager.activity;


import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.view.MenuItemCompat;

import com.example.kaoqinmanager.R;
import com.example.kaoqinmanager.base.BaseBindingActivity;
import com.example.kaoqinmanager.bean.DaKaTable;
import com.example.kaoqinmanager.databinding.ActivityMainBinding;
import com.example.kaoqinmanager.utils.DateUtils;
import com.example.kaoqinmanager.utils.MyToast;

import java.time.format.DateTimeFormatter;
import java.util.Calendar;

import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateUtil;

//公告
public class MainActivity extends BaseBindingActivity<ActivityMainBinding> {

    //是否为上班打卡
    private boolean isWorkDaKa = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        actionBar.setTitle("考勤管理");
        final Calendar mCalendar = Calendar.getInstance();
        mCalendar.setTimeInMillis(System.currentTimeMillis());
        //int hour = mCalendar.get(Calendar.HOUR);
        int apm = mCalendar.get(Calendar.AM_PM);
        //apm=0 表示上午，apm=1表示下午。
        if (apm == 0) {
            isWorkDaKa = true;
            getBinding().tvDaKa.setText("上班打卡");
        } else {
            getBinding().tvDaKa.setText("下班打卡");
            isWorkDaKa = false;
        }


        //公告
        getBinding().tvBulletin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(BulletinActivity.class);
            }
        });


        getBinding().tvRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(RecordActivity.class);
            }
        });

        //请假
        getBinding().tvAskForLeave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(AskForLeaveActivity.class);
            }
        });


        getBinding().tvSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(SettingActivity.class);
            }
        });

        getBinding().flDaKa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //当前时间字符串，格式：yyyy-MM-dd HH:mm:ss
                //String now = DateUtil.now();
                // System.out.println(".onClick now=" + now);

                DaKaTable daKaTable = new DaKaTable();
                getBinding().tvDaKa.setText("下班打卡");

                if (isWorkDaKa) {
                    isWorkDaKa = false;
                    daKaTable.setDakaStatus("上班打卡");
                } else {
                    daKaTable.setDakaStatus("下班打卡");
                }


                String currentDate = DateUtils.getCurrentDate();
                System.out.println(".onClick currentDate=" + currentDate);

                daKaTable.setTime(currentDate);
                daKaTable.setUsername(mSharePreferenceUtil.getUserName());
                daKaTable.setLocation("xxxx地址");

                daKaTable.save();
                MyToast.showToast("打卡成功");


            }
        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.option_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            //actionbar的左侧图标的点击事件处理
            case R.id.action_note:
                openActivity(BulletinActivity.class);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
